package com.canaan.business.service.inOutStock.impl;

import java.util.Date;
import java.util.List;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.uuid.UUID;
import com.canaan.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.inOutStock.ReturnStockBillMapper;
import com.canaan.business.domain.inOutStock.ReturnStockBill;
import com.canaan.business.service.inOutStock.IReturnStockBillService;
import com.canaan.common.annotation.TableName;
/**
 * 回库单Service业务层处理
 *
 * @author canaan
 * @date 2025-04-02
 */
@Service
public class ReturnStockBillServiceImpl implements IReturnStockBillService {
    @Resource
    private ReturnStockBillMapper returnStockBillMapper;

    /**
     * 查询回库单
     *
     * @param id 回库单主键
     * @return 回库单
     */
    @Override
    public ReturnStockBill selectReturnStockBillById(Long id) {
        return returnStockBillMapper.selectReturnStockBillById(id);
    }

    /**
     * 查询回库单列表
     *
     * @param returnStockBill 回库单
     * @return 回库单
     */
    @Override
    public List<ReturnStockBill> selectReturnStockBillList(ReturnStockBill returnStockBill) {
        return returnStockBillMapper.selectReturnStockBillList(returnStockBill);
    }

    /**
     * 新增回库单
     *
     * @param returnStockBill 回库单
     * @return 结果
     */
    @Override
    public int insertReturnStockBill(ReturnStockBill returnStockBill) {
        if(StringUtils.isBlank(returnStockBill.getNo())){
            returnStockBill.setNo("BACK"+TableNoUtil.getNo(ReturnStockBill.class));
        }
        returnStockBill.setCreateBy(SecurityUtils.getUsername());
        returnStockBill.setCreateTime(DateUtils.getNowDate());
        return returnStockBillMapper.insertReturnStockBill(returnStockBill);
    }

    /**
     * 批量新增回库单
     *
     * @param returnStockBillList 回库单列表
     * @return 结果
     */
    @Override
    public int batchInsertReturnStockBill(List<ReturnStockBill> returnStockBillList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (ReturnStockBill returnStockBill : returnStockBillList){
            if(StringUtils.isBlank(returnStockBill.getNo())){
                returnStockBill.setNo("BACK"+TableNoUtil.getNo(ReturnStockBill.class));
            }
            returnStockBill.setCreateBy(username);
            returnStockBill.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<returnStockBillList.size();) {
            int endIndex = i+500;
            if (endIndex > returnStockBillList.size()) {
                endIndex = returnStockBillList.size();
            }
            insertNum = insertNum + returnStockBillMapper.batchInsertReturnStockBill(returnStockBillList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改回库单
     *
     * @param returnStockBill 回库单
     * @return 结果
     */
    @Override
    public int updateReturnStockBill(ReturnStockBill returnStockBill) {
        if(StringUtils.isEmpty(returnStockBill.getUpdateBy())){
            returnStockBill.setUpdateBy(SecurityUtils.getUsername());
        }
        returnStockBill.setUpdateTime(DateUtils.getNowDate());
        return returnStockBillMapper.updateReturnStockBill(returnStockBill);
    }

    /**
     * 批量修改回库单
     *
     * @param returnStockBillList 回库单列表
     * @return 结果
     */
    @Override
    public int batchUpdateReturnStockBill(List<ReturnStockBill> returnStockBillList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (ReturnStockBill returnStockBill : returnStockBillList){
            returnStockBill.setUpdateBy(username);
            returnStockBill.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<returnStockBillList.size();) {
            int endIndex = i+500;
            if (endIndex > returnStockBillList.size()) {
                endIndex = returnStockBillList.size();
            }
            updateNum = updateNum + returnStockBillMapper.batchUpdateReturnStockBill(returnStockBillList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除回库单
     *
     * @param ids 需要删除的回库单主键集合
     * @return 结果
     */
    @Override
    public int deleteReturnStockBillByIds(Long[] ids) {
        return returnStockBillMapper.deleteReturnStockBillByIds(ids);
    }

    /**
     * 删除回库单信息
     *
     * @param id 回库单主键
     * @return 结果
     */
    @Override
    public int deleteReturnStockBillById(Long id) {
        return returnStockBillMapper.deleteReturnStockBillById(id);
    }

    /**
     * 查询回库单
     *
     * @param no 编号
     * @return 回库单
     */
    @Override
    public ReturnStockBill selectReturnStockBillByNo(String no) {
        return returnStockBillMapper.selectReturnStockBillByNo(no);
    }

    /**
     * 批量删除回库单
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteReturnStockBillByNos(String[] nos) {
        return returnStockBillMapper.deleteReturnStockBillByNos(nos);
    }

    /**
     * 删除回库单信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteReturnStockBillByNo(String no) {
        return returnStockBillMapper.deleteReturnStockBillByNo(no);
    }
}
